package com.lincoln.system.app.model;

import com.lincoln.entity.BaseEntityVO;
import com.lincoln.entity.role.DbRole;
import com.lincoln.entity.user.DbUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserVO extends BaseEntityVO {
    String userName;
    String roleName;
    DbRole role;
    boolean enabled;
    boolean accountNonLocked;
    boolean accountNonExpired;
    boolean credentialsNonExpired;

    public UserVO(DbUser user) {
        if (user != null) {
            id = user.getId();
            createTime = user.getCreateTime();
            updateTime = user.getUpdateTime();
            updateKey = user.getUpdateKey();
            userName = user.getUserName();
            enabled = user.isEnabled();
            accountNonExpired = user.isAccountNonExpired();
            accountNonLocked = user.isAccountNonLocked();
            credentialsNonExpired = user.isCredentialsNonExpired();
            if (user.getRole() != null) {
                roleName = user.getRole().getName();
                role = user.getRole();
            }
        }
    }

    /**
     * page转换
     * @param page
     * @return
     */
    public static Page<UserVO> parsePageUserVO(Page<DbUser> page){
        if(page == null){
            return null;
        }
        List<DbUser> list = page.getContent();
        List<UserVO> data = new ArrayList<>();
        for(DbUser dbUser : list){
            UserVO userVO = new UserVO(dbUser);
            data.add(userVO);
        }
        Page<UserVO> result = new PageImpl<>(data,page.getPageable(),page.getTotalElements());
        return result;
    }
}
